<template>
  <div class="icons">
    <swiper :options="swiperOption">
      <swiper-slide v-for="(page, index) of pages" :key="index">
        <div class="icon" v-for="item of page" :key="item.id">
          <div class="icon-img">
            <img class="icon-img-content" :src="item.imgUrl" alt="icon">
          </div>
          <p class="icon-desc">{{item.desc}}</p>
        </div>
      </swiper-slide>
    </swiper>
  </div>
</template>

<script>
export default {
  name: 'HomeIcons',
  props: {
    list: Array
  },
  data () {
    return {
      swiperOption: {
        autoplay: false
      }
    }
  },
  computed: {
    // 通过computed属性，将图标数据拆分成一个二维数组
    pages () {
      let pages = []
      this.list.forEach((item, index) => {
        let page = Math.floor(index / 8) // page为计算的到的页码， index的0-7的页码均为 0 ，8-10的页码为 1
        if (!pages[page]) {
          pages[page] = [] // 控制创建二维数组，为空则创建
        }
        pages[page].push(item) // 将数据push进相应的二维数组
      })
      // console.log(pages)
      // 需要将数据return
      return pages
    }
  }
}
</script>

<style lang="stylus" scoped>
  // padding-bottom是相对于width的
  @import '~styles/variables.styl'
  @import '~styles/mixins.styl'
  .icons >>> .swiper-container
    height 0
    padding-bottom 50%
  .icons
    margin-top 0.1rem
    .icon
      position relative
      overflow hidden
      float left
      width 25%
      height 0
      padding-bottom 25%
      .icon-img
        position absolute
        top 0
        left 0
        right 0
        bottom 0.44rem
        box-sizing border-box
        padding 0.1rem
        .icon-img-content
          height 100%
          display block
          margin 0 auto
      .icon-desc
        position absolute
        left 0
        right 0
        bottom 0
        height 0.44rem
        line-height 0.44rem
        text-align center
        color $darkTextColor
        ellipsis()
</style>
